package com.sduept.nwld.dataserver.interceptors;

import com.sduept.bigdata.fault.entity.FaultRecord;
import com.sduept.bigdata.fault.entity.FaultRecordWrapper;
import com.sduept.bigdata.weather.entity.TyphoonFaultInfo;
import com.sduept.bigdata.weather.manager.TyphoonFaultInfoManager;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 故障拦截器
 * @date 2018-09-26
 */
@Slf4j
@Aspect
@Service
public class FaultRecordInterceptor {

	@Autowired
	private TyphoonFaultInfoManager typhoonFaultInfoManager;

	@Pointcut("execution(* com.sduept.bigdata.fault.manager.FaultRecordPersistManager.*(..))")
	public void declareJoinPointExpression(){


	}



	@Around(value = "declareJoinPointExpression()")
	public Object methodInterceptor(ProceedingJoinPoint ctx) throws Exception {
		
		String methodName = ctx.getSignature().getName();//方法名

		Object proceed = null;
		try {
			proceed = ctx.proceed();
		} catch (Throwable throwable) {
			throwable.printStackTrace();
		}
		if("deleteFaultRecord".equals(methodName)){//删除
			
			Object[] parameter = ctx.getArgs();
			String faultId = (String) parameter[0];
			typhoonFaultInfoManager.deleteInfoByFaultId(faultId);

		}
		if("saveFaultRecord".equals(methodName)){//保存
			
			Object[] parameter = ctx.getArgs();

			FaultRecordWrapper faultRecordWrapper = (FaultRecordWrapper) parameter[0];
			FaultRecord faultRecord = faultRecordWrapper.getFaultRecord();
			List<TyphoonFaultInfo> infoList = typhoonFaultInfoManager.findByFaultId(faultRecord.getId());
			if(infoList.size() == 0){//信息表中无数据，需要计算
				
				List<FaultRecord> faultList = new ArrayList<FaultRecord>();
				faultList.add(faultRecord);
				typhoonFaultInfoManager.calculateTyphoonFaultInfo(faultList);
			}
		}

		return proceed;
	}
}
